package com.sx.pipeline.ui.wallet.withdraw.record.list

import android.os.Bundle
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.BalanceRecordAdapter
import com.sx.pipeline.adapter.WithdrawRecordAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseFragment
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.databinding.FragWalletRecordBinding
import com.sx.pipeline.repos.http.res.BalanceRecordVO
import com.sx.pipeline.repos.http.res.WithdrawRecordVO
import com.sx.pipeline.ui.wallet.WalletVM
import com.sx.pipeline.ui.wallet.withdraw.WithdrawVM


/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description:
 */
class WithdrawRecordFragment :
    CYDataBindingBaseFragment<FragWalletRecordBinding, WithdrawVM>(R.layout.frag_wallet_record){
    private lateinit var mAdapter: WithdrawRecordAdapter
    private var status = 0 //状态

    companion object {
        fun newInstance(status: Int) =
            WithdrawRecordFragment().apply {
                arguments = Bundle().apply {
                    putInt(IntentKey.ID, status)
                }
            }
    }

    override fun initData() {
        status = arguments?.getInt(IntentKey.ID)!!
        mViewModel.findWithdrawByPage(status, mPage)
    }

    override fun initListener() {
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mPage++
                mViewModel.findWithdrawByPage(status)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mPage = 1
                mViewModel.findWithdrawByPage(status)
            }
        })
    }


    override fun initViewObservable() {
        super.initViewObservable()

        // 刷新事件
        observe(mViewModel.mUI.initRefreshEvent()) {
            mBinding.refreshLayout.finishRefresh(it!!)
        }

        // 加载事件
        observe(mViewModel.mUI.initLoadEvent()) {
            mBinding.refreshLayout.finishLoadMore(it!!)
        }

        // 需求列表列表
        setNeedsList()
    }

    private fun setNeedsList() {
        observe(mViewModel.withRecordListEvent) {
            if (it != null && !it.records.isNullOrEmpty()) {
                mBinding.emptyView.visibility = View.GONE
                mBinding.recyclerView.visibility = View.VISIBLE
                if (!this::mAdapter.isInitialized) {
                    mBinding.recyclerView.layoutManager = LinearLayoutManager(
                        context, LinearLayoutManager.VERTICAL, false
                    )
                    mAdapter =
                        WithdrawRecordAdapter(it.records as MutableList<WithdrawRecordVO>)
                    mBinding.recyclerView.adapter = mAdapter
                    mAdapter.setOnItemClickListener { adapter, view, position ->

                    }



                } else {
                    mBinding.refreshLayout.setEnableLoadMore(true)
                    if (mPage == 1) {
                        mAdapter.setNewInstance(it.records as MutableList<WithdrawRecordVO>)
                    } else {
                        mAdapter.addData(it.records)
                    }
                }
                if (mAdapter.data.size == it.total) {
                    mBinding.refreshLayout.setEnableLoadMore(false)
                }
            } else {
                mBinding.refreshLayout.setEnableLoadMore(false)
                mBinding.emptyView.visibility = View.VISIBLE
                mBinding.recyclerView.visibility=View.GONE
            }
        }
    }

}